﻿using System;
using System.Linq;
using NeuralLib.ActivationFunction;

namespace NeuralLib
{
    [Serializable]
    public class Layer
    {
        protected int _inputsCount = 0;

        protected Neuron[] _neurons = null;

        protected int _neuronsCount = 0;

        protected double[] _output = null;

        public Layer(int neuronsCount, int inputsCount, IActivationFunction function)
        {
            _inputsCount = inputsCount;
            _neuronsCount = neuronsCount;
            _neurons = new Neuron[neuronsCount];

            for (var i = 0; i < neuronsCount; i++)
                _neurons[i] = new Neuron(inputsCount, function);
            _output = new double[_neuronsCount];
        }

        public int InputsCount
        {
            get
            {
                return _inputsCount;
            }
        }

        public Neuron[] Neurons
        {
            get
            {
                return _neurons;
            }
        }

        public int NeuronsCount
        {
            get
            {
                return _neuronsCount;
            }
        }

        public double[] Output
        {
            get
            {
                return _output;
            }
        }

        public double[] Compute(double[] input)
        {
            if (_inputsCount != input.Count())
                throw new ArgumentOutOfRangeException("input", "Not correct input array size");

            var output = new double[NeuronsCount];

            for (var i = 0; i < _neuronsCount; i++)
                output[i] = _neurons[i].Compute(input);

            _output = output;

            return output;
        }
    }
}